Enum field


In [1]:
from konfoo import Index, Byteorder, Enumeration, Enum, Enum8, Enum16, Enum24, Enum32, Enum64

Item

Item type of the field class.


In [2]:
Enum.item_type


Out[2]:
ItemClass.Enum = 48

Checks if the field class is a bit field.


In [3]:
Enum.is_bit()


Out[3]:
False

Checks if the field class is a boolean field.


In [4]:
Enum.is_bool()


Out[4]:
False

Checks if the field class is a decimal number field.


In [5]:
Enum.is_decimal()


Out[5]:
True

Checks if the field class is a floating point number field.


In [6]:
Enum.is_float()


Out[6]:
False

Checks if the field class is a pointer field.


In [7]:
Enum.is_pointer()


Out[7]:
False

Checks if the field class is a stream field.


In [8]:
Enum.is_stream()


Out[8]:
False

Checks if the field class is a string field.


In [9]:
Enum.is_string()


Out[9]:
False

Field


In [10]:
class Validity(Enumeration):
    error = 0
    correct = 1
    forced = 2
    undefined = 3

In [11]:
enum = Enum(bit_size=2, align_to=None, enumeration=None, byte_order='auto')

In [12]:
enum = Enum(2, enumeration=Validity)

Field view


In [13]:
enum


Out[13]:
Enum(index=Index(byte=0, bit=0, address=0, base_address=0, update=False), alignment=Alignment(byte_size=1, bit_offset=0), bit_size=2, value='error')

In [14]:
str(enum)


Out[14]:
'Enum2(Index(byte=0, bit=0, address=0, base_address=0, update=False), Alignment(byte_size=1, bit_offset=0), 2, error)'

In [15]:
repr(enum)


Out[15]:
"Enum(index=Index(byte=0, bit=0, address=0, base_address=0, update=False), alignment=Alignment(byte_size=1, bit_offset=0), bit_size=2, value='error')"

Field name


In [16]:
enum.name


Out[16]:
'Enum2'

Field index


In [17]:
enum.index


Out[17]:
Index(byte=0, bit=0, address=0, base_address=0, update=False)

Byte index of the field within the byte stream.


In [18]:
enum.index.byte


Out[18]:
0

Bit offset relative to the byte index of the field within the byte stream.


In [19]:
enum.index.bit


Out[19]:
0

Absolute address of the field within the data source.


In [20]:
enum.index.address


Out[20]:
0

Base address of the byte stream within the data source.


In [21]:
enum.index.base_address


Out[21]:
0

Indexes the field and returns the index after the field.


In [22]:
enum.index_field(index=Index())


Out[22]:
Index(byte=0, bit=2, address=0, base_address=0, update=False)

Field alignment


In [23]:
enum.alignment


Out[23]:
Alignment(byte_size=1, bit_offset=0)

Byte size of the field group which the field is aligned to.


In [24]:
enum.alignment.byte_size


Out[24]:
1

Bit offset of the field within its aligned field group.


In [25]:
enum.alignment.bit_offset


Out[25]:
0

Field size


In [26]:
enum.bit_size


Out[26]:
2

Field byte order


In [27]:
enum.byte_order


Out[27]:
Byteorder.auto = 'auto'

In [28]:
enum.byte_order.value


Out[28]:
'auto'

In [29]:
enum.byte_order.name


Out[29]:
'auto'

In [30]:
enum.byte_order = 'auto'

In [31]:
enum.byte_order = Byteorder.auto

Field value

Checks if the decimal field is signed or unsigned.


In [32]:
enum.signed


Out[32]:
False

Maximal decimal field value.


In [33]:
enum.max()


Out[33]:
3

Minimal decimal field value.


In [34]:
enum.min()


Out[34]:
0

Returns the enum field value as an enum name string or an unsigned integer number.


In [35]:
enum.value


Out[35]:
'error'

Returns the decimal field value aligned to its field group as a number of bytes.


In [36]:
bytes(enum)


Out[36]:
b'\x00'

In [37]:
bytes(enum).hex()


Out[37]:
'00'

Returns the decimal field value as an integer number.


In [38]:
int(enum)


Out[38]:
0

Returns the decimal field value as an floating point number.


In [39]:
float(enum)


Out[39]:
0.0

Returns the decimal field value as a lowercase hexadecimal string prefixed with 0x.


In [40]:
hex(enum)


Out[40]:
'0x0'

Returns the decimal field value as a binary string prefixed with 0b.


In [41]:
bin(enum)


Out[41]:
'0b0'

Returns the decimal field value as an octal string prefixed with 0o.


In [42]:
oct(enum)


Out[42]:
'0o0'

Returns the decimal field value as a boolean value.


In [43]:
bool(enum)


Out[43]:
False

Returns the decimal field value as a signed integer number.


In [44]:
enum.as_signed()


Out[44]:
0

Returns the decimal field value as an unsigned integer number.


In [45]:
enum.as_unsigned()


Out[45]:
0

Field metadata

Returns the meatadata of the field as an ordered dictionary.


In [46]:
enum.describe()


Out[46]:
OrderedDict([('address', 0),
             ('alignment', [1, 0]),
             ('class', 'Enum2'),
             ('index', [0, 0]),
             ('max', 3),
             ('min', 0),
             ('name', 'Enum2'),
             ('order', 'auto'),
             ('signed', False),
             ('size', 2),
             ('type', 'Field'),
             ('value', 'error')])

Deserialize


In [47]:
enum.deserialize(bytes.fromhex('01'), byte_order='little')


Out[47]:
Index(byte=0, bit=2, address=0, base_address=0, update=False)

In [48]:
enum.value


Out[48]:
'correct'

In [49]:
bytes(enum)


Out[49]:
b'\x01'

In [50]:
bytes(enum).hex()


Out[50]:
'01'

In [51]:
int(enum)


Out[51]:
1

In [52]:
float(enum)


Out[52]:
1.0

In [53]:
hex(enum)


Out[53]:
'0x1'

In [54]:
bin(enum)


Out[54]:
'0b1'

In [55]:
oct(enum)


Out[55]:
'0o1'

In [56]:
bool(enum)


Out[56]:
True

Serialize


In [57]:
buffer = bytearray()

In [58]:
enum.value = 1

In [59]:
enum.value = 1.0

In [60]:
enum.value = 0x1

In [61]:
enum.value = 0b1

In [62]:
enum.value = 0o1

In [63]:
enum.value = True

In [64]:
enum.value = 'correct'

In [65]:
enum.serialize(buffer, byte_order='little')


Out[65]:
Index(byte=0, bit=2, address=0, base_address=0, update=False)

In [66]:
buffer.hex()


Out[66]:
'01'

In [67]:
bytes(enum).hex()


Out[67]:
'01'

In [ ]: